package com.globo.opensocial;

import jargs.gnu.CmdLineParser;
import jargs.gnu.CmdLineParser.IllegalOptionValueException;
import jargs.gnu.CmdLineParser.UnknownOptionException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class AntTask extends Task {

	private String charset;
	private String host;
	private String todir;
	private boolean debug;
	private boolean verbose;
	private String application;
	private boolean generateStandAlone;

	// The method executing the task
	public void execute() throws BuildException {

		// handle attribute 'fail'
		// if (fail) throw new BuildException("Fail requested.");

		if ( application != null ) {
			genApp(application);
			return;
		}
		
		// handle nested elements
		for (Iterator it = scripts.iterator(); it.hasNext();) {
			Script script = (Script) it.next();
			genApp(script.getFile());
			//System.out.println(script.getFile());
		}
	}
		
		
	private void genApp( String code ) {
		
		Application app;
		
		Application.clearInstance();

		try {
			app = new Application();

			app.setDebug(debug);

			if (charset == null || !Charset.isSupported(charset)) {
				charset = System.getProperty("charset");
				if (charset == null) {
					charset = "UTF-8";
				}
			}

			if (verbose) {
				System.out.println("[INFO] Using charset " + charset);
			}
			app.setCharset(charset);

			// Configure the output file name.

			if (!"".equals(todir)) {
				app.setOutput(todir);
			}

			// Set the basehost.
			if (host != null) {
				app.setHost(host);
			} else {
				// usage();
			}

			if (verbose) {
				System.out.println("[INFO] Host: " + host);
			}

			File file = new File(code);
			if (!file.isFile()) {
				System.out.println("[ERROR] File " + application);
				return;
			}

			if (verbose) {
				System.out.println("[INFO] Loading " + application);
			}
			app.setVerbose(verbose);
			app.setCode(file);

			app.build();
			app.write();

			//if(generateStandAlone)
				//app.writeStandAloneJs();

		} catch (IOException e) {
			System.err.println("[ERRO] Erro on built application ");
			e.printStackTrace();
		} catch (Exception e) {
			System.err.println("[ERRO] Erro on built application ");
			e.printStackTrace();
		}
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getTodir() {
		return todir;
	}

	public void setTodir(String todir) {
		this.todir = todir;
	}

	public void setVerbose(Boolean verbose) {
		this.verbose = verbose;
	}

	public void setApplication(String application) {
		this.application = application;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	/** Store nested 'message's. */
	Vector<Script> scripts = new Vector<Script>();

	/** Factory method for creating nested 'message's. */
	public Script createScript() {
		Script script = new Script();
		scripts.add(script);
		return script;
	}

	public void setGenerateStandAlone(boolean generateStandAlone) {
		this.generateStandAlone = generateStandAlone;
	}


	public boolean isGenetateStandAlone() {
		return generateStandAlone;
	}

	/** A nested 'message'. */
	public class Script {
		// Bean constructor
		public Script() {
		}

		String file;

		public void setFile(String file) {
			this.file = file;
		}

		public String getFile() {
			return file;
		}
	}

}
